﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CarPark.Helper;

namespace CarPark.MainModel
{
    public partial class MidCarPark
    {
        public string ParkName(int parkId, ref string pname)
        {
            string sql = "select F_Name from TPark where F_ID = "+parkId;
            try
            {
                pname = SQLHelper.GetDataTable(sql, null, null).Rows[0][0].ToString();
                return GlobalConst.CK_OK;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        public string AvCount(int parkId, ref int count)
        {
            string SQL = "select  F_TotalPos - F_Badpos -(select count(F_ID) from TCar where F_Status = false) as F_AvPos from Tpark WHERE F_ID = "+parkId;
            try
            {
                count = (Int32)Helper.SQLHelper.GetDataTableValue(SQL, "F_AvPos", null, null);
                return GlobalConst.CK_OK;
            }
            catch(Exception E)
            {
                return E.Message;
            }
        }
        public string CarInOut(int parkId, Int64 cardId)
        {
            string SQL = "SELECT * FROM TCar WHERE F_CardID = "+cardId.ToString();
            DataTable dt = Helper.SQLHelper.GetDataTable(SQL,null,null);
            if (dt.Rows.Count > 0)
            {
                //Out
                if ((bool)dt.Rows[0]["F_Status"])
                {
                    SQL = "UPDATE TCar set F_Status = FALSE WHERE F_CardID = "+cardId.ToString();
                    Helper.SQLHelper.DoExecute(SQL,null,null);
                    return AddCost(parkId,cardId,2);
                }
                //In
                else
                {
                    SQL = "UPDATE TCar set F_Status = TRUE WHERE F_CardID = " + cardId.ToString();
                    Helper.SQLHelper.DoExecute(SQL, null, null);
                    return AddCost(parkId,cardId,1);
                }
            }
            return "卡号不存在";
        }
        private string AddCost(int parkid, Int64 cardId,byte type)
        {
            //in
            if (1 == type)
            {
                string SQL = "INSERT INTO TCost (F_ParkID, F_CID, F_BeginTime, F_EndTime, F_Cost)"
                        + "VALUES        (" + parkid.ToString() + ", " + cardId.ToString() + ", '" + DateTime.Now.ToString() + "', '" + DateTime.Now.ToString() + "', '0')";
                if (1 == Helper.SQLHelper.DoExecute(SQL, null, null))
                    return GlobalConst.CK_OK;
                return GlobalConst.PARENT_ACCESS_INVALID;
            }
            //out
            else if (2 == type)
            {
                string timeSql = "update tcost set  F_endtime = '"+DateTime.Now.ToString()+"' where F_ID = (select max(F_ID) from tcost where f_Cid = "+cardId.ToString()+")";
                string priceSql = "select F_CostPerH from T_Price where(F_CarModel = (select F_ModelID from tcar where f_cardID = "+cardId.ToString()+"))";
                int price = Convert.ToInt32( Helper.SQLHelper.GetDataTableValue(priceSql, "F_CostPerH", null, null));
                string costSql = "update tcost set  F_Cost =  datediff('h' ,f_begintime,f_endtime)*"
                    +price.ToString()+" where F_ID = (select max(F_ID) from tcost where f_Cid = "+cardId.ToString()+")";
                if (2 == Helper.SQLHelper.DoExecute(timeSql, null, null) + Helper.SQLHelper.DoExecute(costSql, null, null))
                    return GlobalConst.CK_OK;
                return GlobalConst.PARENT_ACCESS_INVALID;
            }
            else
                return GlobalConst.PARENT_ACCESS_FAILED;
        }
        public string InsideCars(Int64 carNumber, string carTag, DateTime beginDt, DateTime endDt,Byte type, ref DataTable adt)
        {
            string sql = "select c.F_CardID as 卡号,c.F_CarTag as 车牌,"+
                "c.F_CarOwner as 车主,c.F_Status as 状态,"+
                "(select top 1 t.F_BeginTime from tcost t where c.F_cardid "+
                "= t.F_Cid order by t.f_id desc) as 停车时间,"+
                "(select top 1 g.F_EndTime from tcost g where c.F_cardid "+
                "= g.F_Cid order by g.f_id desc)as 出库时间  "+
                "from Tcar c where c.f_cardid = "+carNumber.ToString()+" or c.F_Cartag = '"+carTag+"'";
            if (0 != type)
            {
                sql += " or 1=1";
            }
            try
            {
                adt = Helper.SQLHelper.GetDataTable(sql, null, null);
                return GlobalConst.CK_OK;
            }
            catch
            {
                return GlobalConst.PARENT_ACCESS_FAILED;
            }
        }
    }
}